package com.kun.order.service.impl;

import com.kun.common.utils.R;
import com.kun.order.entity.OrderDetailedEntity;
import com.kun.order.feign.ProductFeignService;
import com.kun.order.service.OrderDetailedService;
import com.kun.order.vo.CountPayTypeVo;
import com.kun.order.vo.MonthSaleByYearVo;
import com.kun.order.vo.OrderDetailedVo;
import com.kun.order.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.order.dao.OrderDao;
import com.kun.order.entity.OrderEntity;
import com.kun.order.service.OrderService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    OrderDetailedService orderDetailedService;
    @Autowired
    ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );
        PageUtils pageUtils = new PageUtils(page);
        List<OrderEntity> records = page.getRecords();
        List<OrderVo> orderVos = records.stream().map((orderEntity) -> {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(orderEntity, orderVo);
            List<OrderDetailedVo> entityList = orderDetailedService.getOrderDestailByOederId(orderEntity.getId().toString());
            orderVo.setOrderDetailedVoList(entityList);
            return orderVo;
        }).collect(Collectors.toList());
        pageUtils.setList(orderVos);
        return pageUtils;
    }

    @Override
    public List<CountPayTypeVo> getCountPayType() {

        return baseMapper.getCountPayType();
    }

    @Override
    public List<MonthSaleByYearVo> getMonthSaleByYear(String year) {
        return baseMapper.getMonthSaleByYear(year);
    }
    @Transactional
    @Override
    public void saveOrderVo(OrderVo orderVo) {
//        OrderEntity orderEntity = new OrderEntity();
//        OrderDetailedEntity orderDetailed = new OrderDetailedEntity();
//        BeanUtils.copyProperties(orderVo,orderEntity);
//        BeanUtils.copyProperties(orderVo,orderDetailed);
//        baseMapper.insert(orderEntity);
//
//        orderDetailed.setParentid(orderEntity.getId());
//        orderDetailed.setPTotalPrice(orderVo.getpTotalPrice());
//        orderDetailedService.save(orderDetailed);
    }

    @Override
    public OrderVo getOrderDestailedById(String id) {
//        OrderVo orderVo = new OrderVo();
//        OrderEntity orderEntity = baseMapper.selectById(id);
//        QueryWrapper<OrderDetailedEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parentid",orderEntity.getId());
//        OrderDetailedEntity orderDetailed = orderDetailedService.getOne(queryWrapper);
//        BeanUtils.copyProperties(orderEntity,orderVo);
//        BeanUtils.copyProperties(orderDetailed,orderVo);
        return null;
    }
    @Transactional
    @Override
    public void updateOrderVoById(OrderEntity orderVo) {
        baseMapper.updateById(orderVo);
    }
    @Transactional
    @Override
    public void removeOrderVoById(String id) {
        baseMapper.deleteById(id);

        QueryWrapper<OrderDetailedEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentid",id);
        orderDetailedService.remove(queryWrapper);
    }

}